import 'package:flutter/material.dart';
import 'package:flutter_app/bean/collect_detail_entity.dart';
import 'package:flutter_app/manager/UserInfoManager.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/utils/EventBusUtils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/watermark/watermark_utils.dart';
import 'package:get/get.dart';

import 'apply_detail_state.dart';

class ApplyDetailLogic extends GetxController {
  final ApplyDetailState state = ApplyDetailState();

  late String collectId;

  String? taskType; // 类型 1明查暗访 2 窗口值班 3 其他任务


  @override
  void onInit() {
    super.onInit();
    collectId = Get.arguments;
  }

  @override
  void onReady() {
    super.onReady();
    WatermarkUtils().addWatermark(Get.context!, UserInfoManager().getUserName());

    postCollectDetail();
  }


  /// 获取报名信息
  Future<void> postCollectDetail() async {
    Map<String, dynamic> param = {
      'id': collectId
    };
    ResultData resultData = await Api.postCollectDetail(param);
    if (resultData.isSuccess) {
      state.collectDetailEntity = CollectDetailEntity.fromJson(resultData.data['data']);
      taskType = state.collectDetailEntity?.taskType;
      String time = state.collectDetailEntity?.createTime ?? "";
      state.createTime = time == "" ? time : time.substring(0, 10);
      state.orgName = state.collectDetailEntity?.orgName ?? "";
      state.taskName = state.collectDetailEntity?.taskName ?? "";
      /// 发送通知
      bus.fire(ApplyEvent(collectId, state.collectDetailEntity?.status ?? "", state.collectDetailEntity?.totalSign ?? ""));
      update();
    }
  }

  /// 立即报名
  Future<bool> postGoCollectSign() async {
    Map<String, dynamic> param = {"id": collectId};
    ResultData resultData = await Api.postGoCollectSign(param);
    if (resultData.isSuccess) {
      updateDynamicEvent();
      return true;
    } else {
      toast.show(resultData.data["message"]);
      return false;
    }
  }


  // 获取button 颜色
  // collectStatus 采集状态  3已结束
  // isLimit 1过期 0未过期
  //  status 报名状态 0未报名 1已报名
  Color getButtonColors() {
    if (state.collectDetailEntity?.collectStatus == "3") {
      return MyColors.FFFDB7A7;
    } else {
      if (state.collectDetailEntity?.isLimit == "0" && state.collectDetailEntity?.status == "0") {
        return MyColors.FFF03F35;
      } else {
        return MyColors.FFFDB7A7;
      }
    }
  }

  // 刷新首页 效能动态数据
  void updateDynamicEvent() {
    bus.fire(DynamicEvent());
  }

  @override
  void onClose() {
    super.onClose();
    WatermarkUtils().removeWatermark();
  }
}
